<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button>添加角色</el-button>
      <el-table
        size="small"
        :data="tableData"
        highlight-current-row
        v-loading="loading"
        style="width: 100%" border
        element-loading-text="拼命加载中">
        <el-table-column align="center" type="selection" width="60">
        </el-table-column>
        <el-table-column sortable prop="roleName" label="角色名称" width="300">
        </el-table-column>
        <el-table-column sortable prop="roleNo" label="角色代码" width="300">
        </el-table-column>
        <el-table-column sortable prop="editTime" label="修改时间" width="300">
<!--          <template slot-scope="scope">-->
<!--            <div>{{scope.row.editTime|timestampToTime}}</div>-->
<!--          </template>-->
        </el-table-column>
        <el-table-column sortable prop="editUser" label="修改人" width="300">
        </el-table-column>
        <el-table-column align="center" label="操作" min-width="300">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="rowDel(scope.$index, scope.row)">删除</el-button>
            <el-button size="mini" type="success" @click="menuAccess(scope.$index, scope.row)">菜单权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination v-bind:child-msg="page" @callFather="callFather"></Pagination>
    </el-card>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form label-width="120px" :model="editForm" ref="editForm" :rules="rules">
          <el-form-item label="系统编码" prop="systemNo">
            <el-input size="small" v-model="editForm.systemNo" auto-complete="off" placeholder="请输入系统编码"></el-input>
          </el-form-item>
          <el-form-item label="角色名称" prop="roleName">
            <el-input size="small" v-model="editForm.roleName" auto-complete="off" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色代码" prop="roleNo">
            <el-input size="small" v-model="editForm.roleNo" auto-complete="off" placeholder="请输入角色代码"></el-input>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- 菜单权限 -->
    <el-dialog title="菜单权限" :visible.sync="menuAccessshow" width="30%" @click='closeDialog("perm")'>
      <el-tree ref="tree" default-expand-all="" :data="RoleRight" :props="RoleRightProps" :default-checked-keys="checkmenu" node-key="id" show-checkbox>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("perm")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="menuPermSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {mapState,mapMutations} from 'vuex'
import Pagination from '../../components/Pagination'
export default {
  /**
   * 注册组件
   */
  components: {
    Pagination
  },
  data () {
    return {
      loading:false,
      query: {
        info: ''
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      //新增编辑表单
      menuAccessshow: false, //菜单权限
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        roleId: '',
        systemNo: '',
        roleNo: '',
        roleName: '',
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        depositBank: [{ required: true, message: '请输入开户行', trigger: 'blur' }],
        accountNumber: [{ required: true, message: '请输入开户行账号', trigger: 'blur' }],
        adress: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      },
      tableData: [],
    }
  },
  /**
   * 进入页面加载数据
   */
  created () {
    this.onLoad()
  },
  mounted() {},
  computed:{
    ...mapState(['sum']),

  },
  /**
   * 自定义
   */
  methods: {
    onLoad(page, params = {}) {
      let res = {
        code: 0,
        msg: null,
        count: 6,
        data: [
          {
            addUser: 'root',
            editUser: 'root',
            addTime: 1519182004000,
            editTime: 1520288426000,
            roleId: 1,
            systemNo: 'pmd',
            roleNo: 'Administrator',
            roleName: '超级管理员'
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1521111376000,
            editTime: 1520678191000,
            roleId: 2,
            systemNo: 'order',
            roleNo: 'admin',
            roleName: '公司管理员'
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1520678221000,
            editTime: 1520678221000,
            roleId: 95,
            systemNo: 'pm',
            roleNo: 'common',
            roleName: '普通用户'
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526349853000,
            editTime: 1526349853000,
            roleId: 96,
            systemNo: '1',
            roleNo: '1',
            roleName: '1'
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526349942000,
            editTime: 1526437443000,
            roleId: 97,
            systemNo: '2',
            roleNo: '2',
            roleName: '2'
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526652148000,
            editTime: 1526652148000,
            roleId: 101,
            systemNo: 'test',
            roleNo: 'demo',
            roleName: '演示角色'
          }
        ]
      }
      this.loading = false
      this.tableData = res.data
      this.page.total = res.count
      //调用接口，接收动态数据
      // getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
      //   const data = res.data.data;
      //   this.page.total = data.total;
      //   this.tableData = data.records;
      //   this.loading = false;
      // });
    },
    // 分页插件事件
    callFather(parm) {
      this.page.currentPage = parm.currentPage
      this.page.pageSize = parm.pageSize
      this.onLoad(this.page)
    },
    // 顶部搜索事件
    search() {
      this.onLoad(this.page,this.query)
    },
    //清空查询条件查询
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        Object.assign(this.editForm, row);
      } else {
        this.title = '添加'
        Object.assign(this.$data.editForm, this.$options.data.call(this).editForm);
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          roleSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.onLoad(this.page)
                this.$message({
                  type: 'success',
                  message: '角色保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('角色保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除角色
    rowDel(index, row) {
      debugger
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          roleDelete(row.roleId)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '角色已删除！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('角色删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 数据权限
    menuAccess(index, row) {
      this.menuAccessshow = true
      /**
       * 获取权限数据
       */
      // RoleRightTree(row.roleId)
      //   .then(res => {
      //     if (res.data.success) {
      //       this.$message({
      //         type: 'success',
      //         message: '获取权限成功'
      //       })
      //       this.changemenu(res.data.data)
      //       this.RoleRight = this.changeArr(res.data.data)
      //     } else {
      //       this.$message({
      //         type: 'info',
      //         message: res.data.message
      //       })
      //     }
      //   })
      //   .catch(err => {
      //     this.loading = false
      //     this.$message.error('获取权限失败，请稍后再试！')
      //   })
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.menuAccessshow = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
